package com.simpleframe.core.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import com.simpleframe.util.Assert;
import com.simpleframe.util.ClassUtil;

public class ClassPathResource extends AbstractResource{
	
	String path;
	
	ClassLoader classLoader;
	
	public ClassPathResource(String path){
		this(path, (ClassLoader)null);
	}
	
	public ClassPathResource(String path, ClassLoader classLoader){
		Assert.notNull(path, "path 不能为空");
		if(classLoader==null){
			this.classLoader = ClassUtil.getDefaultClassLoader();
		}
		this.path = path;
	}
	
	@Override
	public InputStream getInputStream(){
		InputStream is = null;
		
		is = classLoader.getResourceAsStream(path);
		
		return is;
	}
	
	@Override
	public URL getURL(){
		return classLoader.getResource(path);
	}
	
	@Override
	public File getFile(){
		return new File(getURL().getFile());
	}
	
	public static void main(String[] args) throws IOException {
		ClassPathResource classPathResource = new ClassPathResource("test.xml");
		System.out.println(classPathResource.getFile());
		System.out.println(ClassPathResource.class.getResource("test.xml"));
	}
}
